package protocol.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CJLog {
	/**
	 * 控制台输出
	 * */
	private Log logConsole;
	
	/**
	 * 输出日志文件
	 * */
	private Log logFile;
	
	private boolean printLog = false;
	
	private int printMark = 1;
	
	public CJLog(){
		this.logConsole =  LogFactory.getLog("stdout");
		this.logFile = LogFactory.getLog("file");
	}
	
	/**
	 * 创建日志对象
	 * @param printLog 是否输出日志
	 * @param printMark 日志输出方式，1：控制台，2：文件
	 * */
	public static CJLog createLog(boolean printLog,int printMark){
		CJLog log = new CJLog();
		log.printLog = printLog;
		log.printMark = printMark;
		return log;
	}
	
	/**
	 * 创建日志对象
	 * @param printLog 是否输出日志
	 * @param printMark 日志输出方式，1：控制台，2：文件
	 * */
	public static CJLog createLog(){
		CJLog log = new CJLog();
		log.printLog = true;
		log.printMark = 1;
		return log;
	}
	
	public void out(String msg){
		if(this.printLog){
			if(this.printMark == 1){
				this.logConsole.error(msg);
			}else{
				this.logFile.error(msg);
			}
		}
	}
	
	public void out(Exception ex){
		if(this.printLog){
			String msg = StringKit.getExceptionInfo(ex);
			if(this.printMark == 1){
				this.logConsole.error(msg);
			}else{
				this.logFile.error(msg);
			}
		}
	}
	
	public static Log getLogConsole(){
		return LogFactory.getLog("stdout");
	}
	
	public static Log getLogFile(){
		return LogFactory.getLog("file");
	}
}
